var async = require('async');
var moment = require('moment');
var iWant = mongoose.model('user_i_want');
var MasterData = mongoose.model('masterData_Dictionaries');
var Community = mongoose.model('community');
var User = mongoose.model('user');


var Group = mongoose.model('backoffice_user_numbergroup');


exports.findCompatriotsService = function (req, callback) {

    var body = req.body;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;
    var option = {'type':3};

    if (body.communityId) {
        option.community = body.communityId;
    }

    //if (body.cityId) {
    //    option.cityId = {cityId: body.cityId};
    //}

    var sortType = {dateCreated: -1};

    async.series([

        function (callbackInner) {
            iWant.count(option, function (err, count) {
                callbackInner(err, Math.ceil(count / size));
            });
        },
        function (callbackInner) {
            iWant.find(option, '_id community user content dateCreated')
                .populate('community', '_id communityName')
                .populate('user', '_id nickName')
                .sort(sortType).skip(index).limit(size).exec(function (err, results) {

                    if (err) {
                        return callbackInner(err);
                    } else {

                        if (results) {

                            var data = [];
                            results.forEach(function (iwant) {

                                if (iwant.community) {
                                    iwant._doc.communityName = iwant.community.communityName;
                                }

                                if (iwant.dateCreated) {
                                    iwant._doc.dateCreated = iwant.dateCreated;
                                }

                                if (iwant.content && iwant.content.length > 0) {
                                    iwant._doc.provinceName = iwant.content[0];
                                }

                                if (iwant.content && iwant.content.length > 1) {
                                    iwant._doc.cityName = iwant.content[1];
                                }

                                if (iwant.user) {
                                    iwant._doc.createdByNickName = iwant.user.nickName;
                                }

                                data.push(iwant);
                            });

                        }
                        return callbackInner(null, data);
                    }

                })
        }], callback);

};




exports.findPlaymatesService = function (req, callback) {

    var body = req.body;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;

    var option = {'type':1};
    if (body.communityId) {
        option.community = body.communityId;
    }

    if (body.sex == 'male') {
        option.content = '男';
    }else if (body.sex == 'female') {
        option.content = '女';
    }



    var sortType = {dateCreated: -1};


    async.series([

        function (callbackInner) {
            iWant.count(option, function (err, count) {
                callbackInner(err, Math.ceil(count / size));
            });
        },
        function (callbackInner) {
            iWant.find(option, '_id community user content dateCreated')
                .populate('community', '_id communityName')
                .populate('user', '_id nickName')
                .sort(sortType).skip(index).limit(size).exec(function (err, results) {

                    if (err) {
                        return callbackInner(err);
                    } else {

                        if (results) {

                            var data = [];
                            results.forEach(function (iwant) {

                                if (iwant.community) {
                                    iwant._doc.communityName = iwant.community.communityName;
                                }

                                if (iwant.dateCreated) {
                                    iwant._doc.dateCreated = iwant.dateCreated;
                                }

                                if (iwant.content && iwant.content.length > 0) {
                                    iwant._doc.sexuality = iwant.content[0];
                                }

                                if (iwant.content && iwant.content.length > 1) {
                                    iwant._doc.birthday = iwant.content[1];
                                }

                                if (iwant.user) {
                                    iwant._doc.createdByNickName = iwant.user.nickName;
                                }

                                data.push(iwant);
                            });

                        }
                        return callbackInner(null, data);
                    }

                })
        }], callback);

};



exports.dosthTogetherService = function (req, callback) {

    var body = req.body;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;
    var option = {'type':2};

    if (body.communityId) {
        option.community = body.communityId;
    }

    if (body.type) {
        option.content = body.type;
    }


    var sortType = {dateCreated: -1};
    async.series([

        function (callbackInner) {
            iWant.count(option, function (err, count) {
                callbackInner(err, Math.ceil(count / size));
            });
        },
        function (callbackInner) {
            iWant.find(option, '_id community user content dateCreated')
                .populate('community', '_id communityName')
                .populate('user', '_id nickName')
                .sort(sortType).skip(index).limit(size).exec(function (err, results) {

                    if (err) {
                        return callbackInner(err);
                    } else {

                        if (results) {

                            var data = [];
                            results.forEach(function (iwant) {

                                if (iwant.community) {
                                    iwant._doc.communityName = iwant.community.communityName;
                                }

                                if (iwant.dateCreated) {
                                    iwant._doc.dateCreated = iwant.dateCreated;
                                }

                                if (iwant.content && iwant.content.length > 0) {
                                    iwant._doc.type = iwant.content[0];
                                }


                                if (iwant.user) {
                                    iwant._doc.createdByNickName = iwant.user.nickName;
                                }

                                data.push(iwant);
                            });

                        }
                        return callbackInner(null, data);
                    }

                })
        }], callback);

};




exports.dosthConfigService = function (req, callback) {

    var body = req.body;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;

    var option = {category:'04'};
    var sortType = {displayOrder: 1};

    async.series([

        function (callbackInner) {
            MasterData.count(option, function (err, count) {
                callbackInner(err, Math.ceil(count / size));
            });
        },

        function (callbackInner) {
            MasterData.find(option, '_id value dateLastModified')
                .sort(sortType).skip(index).limit(size).exec(function (err, results) {

                    if (err) {
                        return callbackInner(err);
                    } else {

                        if (results) {

                            var data = [];
                            results.forEach(function (masterdata) {

                                if (masterdata._id) {
                                    masterdata._doc.id = masterdata._id;
                                }
                                if (masterdata.value) {
                                    masterdata._doc.value = masterdata.value;
                                }
                                if (masterdata.dateLastModified) {
                                    masterdata._doc.dateLastModified = masterdata.dateLastModified;
                                }

                                data.push(masterdata);
                            });

                        }
                        return callbackInner(null, data);
                    }

                })
        }], callback);
};



exports.dosthConfigAddService = function (req, callback) {
    var body = req.body;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;

    var option = {category:'04'};
    var sortType = {displayOrder: 1};

    async.series([

        function (callbackInner) {

            var data = new MasterData();
            data.category = '04';
            if (body.type) {
                data.value = body.type;
            }
            data.displayOrder = moment().format('YYYYMMDDHHmmss');
            data.save(callbackInner);
        },

        function (callbackInner) {
            MasterData.count(option, function (err, count) {
                callbackInner(err, Math.ceil(count / size));
            });
        },

        function (callbackInner) {
            MasterData.find(option, '_id value dateLastModified')
                .sort(sortType).skip(index).limit(size).exec(function (err, results) {

                    if (err) {
                        return callbackInner(err);
                    } else {

                        if (results) {

                            var data = [];
                            results.forEach(function (masterdata) {

                                if (masterdata._id) {
                                    masterdata._doc.id = masterdata._id;
                                }
                                if (masterdata.value) {
                                    masterdata._doc.value = masterdata.value;
                                }
                                if (masterdata.dateLastModified) {
                                    masterdata._doc.dateLastModified = masterdata.dateLastModified;
                                }

                                data.push(masterdata);
                            });

                        }
                        return callbackInner(null, data);
                    }

                })
        }], callback);
}



exports.dosthConfigDeleteService = function (req, callback) {

    var body = req.body;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;

    var option = {category:'04', _id: body.id};
    var sortType = {displayOrder: 1};

    async.series([

        function (callbackInner) {

            MasterData.remove(option, function(err) {
                option = {category:'04'};
                callbackInner(err);
            });
        },

        function (callbackInner) {
            MasterData.count(option, function (err, count) {
                callbackInner(err, Math.ceil(count / size));
            });
        },

        function (callbackInner) {
            MasterData.find(option, '_id value dateLastModified')
                .sort(sortType).skip(index).limit(size).exec(function (err, results) {

                    if (err) {
                        return callbackInner(err);
                    } else {

                        if (results) {

                            var data = [];
                            results.forEach(function (masterdata) {

                                if (masterdata._id) {
                                    masterdata._doc.id = masterdata._id;
                                }
                                if (masterdata.value) {
                                    masterdata._doc.value = masterdata.value;
                                }
                                if (masterdata.dateLastModified) {
                                    masterdata._doc.dateLastModified = masterdata.dateLastModified;
                                }

                                data.push(masterdata);
                            });

                        }
                        return callbackInner(null, data);
                    }

                })
        }], callback);
};


exports.dosthGetTypeeService = function (req, callback) {


    var option = {category: '04'};
    var sortType = {displayOrder: 1};

    async.series([


        function (callbackInner) {
            MasterData.find(option, '_id value')
                .sort(sortType).exec(function (err, results) {

                    if (err) {
                        return callbackInner(err);
                    } else {

                        if (results) {

                            var data = [];
                            results.forEach(function (masterdata) {

                                if (masterdata._id) {
                                    masterdata._doc.id = masterdata._id;
                                }
                                if (masterdata.value) {
                                    masterdata._doc.value = masterdata.value;
                                }

                                data.push(masterdata);
                            });

                        }
                        return callbackInner(null, data);
                    }

                })
        }], callback);
};





exports.testService = function (req, callback) {

    var option = {};
    var sortType = {dateCreated: -1};

    async.series([

        function (callbackInner) {
            Group.find(option)
                .sort(sortType).exec(function (err, results) {

                    if (err) {
                        return callbackInner(err);
                    } else {
                        var data = [];

                        if (results) {
                            data = results;
                        }
                        return callbackInner(null, data);
                    }

                })
        }], callback);
};